package com.eightworld3.supermarket.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eightworld3.supermarket.domain.UserInfo;
import com.eightworld3.supermarket.mapper.UserInfoMapper;
import com.eightworld3.supermarket.service.AssetsEnum;
import com.eightworld3.supermarket.service.UserInfoService;
import com.eightworld3.supermarket.service.dto.UserInfoDTO;
import com.eightworld3.supermarket.service.dto.add.UserInfoAddDTO;
import com.eightworld3.supermarket.service.dto.result.ResponseResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author EightWorld3
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper,UserInfo> implements UserInfoService {
    private final UserInfoMapper userInfoMapper;

    /**
     * UserInfoService 构造方法
     * @param userInfoMapper 用户Mapper
     */
    public UserInfoServiceImpl(UserInfoMapper userInfoMapper){
        this.userInfoMapper = userInfoMapper;
    }

    /**
     * 查询所有
     * @return 返回所有UserInfo(用户)
     */
    @Override
    public List<UserInfo> selectAll(){
        return userInfoMapper.selectList(null);
    }

    /**
     * 根据userId查询UserInfo
     * @param userId 用户id
     * @return 返回userId对应的UserInfo对象(经ResponseResult包装类修饰)
     */
    @Override
    public ResponseResult selectByUserId(Integer userId){
        // Swagger自动排除，目前不触发
        if (userId == null) {
            return new ResponseResult(2, "null");
        }

        UserInfo entity = userInfoMapper.selectById(userId);
        if (entity == null) {
            return new ResponseResult(2, "没有该对象");
        }

        return new ResponseResult(1,"成功",entity);
    }

    /**
     * 新增UserInfo
     * @param userInfoAddDTO UserInfo新增DTO对象
     * @return 返回当前当次新增状况(经ResponseResult包装类修饰)
     */
    @Override
    public ResponseResult addUserInfo(UserInfoAddDTO userInfoAddDTO){
        UserInfo entity = new UserInfo();

        // 用户名字符大小限制
        if(userInfoAddDTO.getUserName() == null){
            return new ResponseResult(2, "用户名不可为空");
        }else if(userInfoAddDTO.getUserName().getBytes().length > AssetsEnum.NAME_VARCHAR_LIMIT.value()){
            return new ResponseResult(2, "用户名过长，字符大小需 <= 32Byte");
        }else {
            entity.setUserName(userInfoAddDTO.getUserName());
        }

        // 密码长度需大于6个字符
        if(userInfoAddDTO.getUserPassword().getBytes().length > AssetsEnum.MIN_PASSWORD_LENGTH_LIMIT.value()){
            return new ResponseResult(2, "密码长度过长，字符大小需 <= 32Byte");
        }else if(userInfoAddDTO.getUserPassword().length() < AssetsEnum.MIN_PASSWORD_LENGTH_LIMIT.value()){
            return new ResponseResult(2, "密码长度需大于6位数");
        }else{
            entity.setUserPassword(userInfoAddDTO.getUserPassword());
        }

        // 用户权限
        if(userInfoAddDTO.getUserPopedom() == AssetsEnum.NORMAL_USER_MARK.value()){
            entity.setUserPopedom(userInfoAddDTO.getUserPopedom());
        }else if(userInfoAddDTO.getUserPopedom() == AssetsEnum.USER_LOCK_MARK.value()){
            entity.setUserPopedom(userInfoAddDTO.getUserPopedom());
        }else {
            entity.setUserPopedom(0);
        }

        // 联系方式字符大小限制
        if (userInfoAddDTO.getUserTelephone().getBytes().length > AssetsEnum.TELEPHONE_NUM_VARCHAR_LIMIT.value()){
            return new ResponseResult(2, "联系方式过长，字符大小需 <= 14Byte");
        }else {
            entity.setUserTelephone(userInfoAddDTO.getUserTelephone());
        }

        // 备注内容字符限制
        if(userInfoAddDTO.getUserRemark().getBytes().length > AssetsEnum.REMARK_VARCHAR_LIMIT.value()){
            return new ResponseResult(2, "备注内容过多，需 <= 120Byte");
        }else {
            entity.setUserRemark(userInfoAddDTO.getUserRemark());
        }

        return new ResponseResult(1,"成功",userInfoMapper.insert(entity));
    }

    /**
     * 更新UserInfo
     * @param userInfoDTO UserInfo DTO对象
     * @return 返回当前当次更新状况(经ResponseResult包装类修饰)
     */
    @Override
    public ResponseResult updateUserInfo(UserInfoDTO userInfoDTO){
        UserInfo entity = new UserInfo();

        // 修改对象存在判断
        UserInfo existEntity = userInfoMapper.selectById(userInfoDTO.getUserId());
        if (existEntity == null){
            return new ResponseResult(2, "用户对象不存在");
        }else {
            entity.setUserId(userInfoDTO.getUserId());
        }

        // 用户名字符大小限制
        if(userInfoDTO.getUserName().getBytes().length > AssetsEnum.NAME_VARCHAR_LIMIT.value()){
            return new ResponseResult(2, "用户名过长，字符大小需 <= 32Byte");
        }else {
            entity.setUserName(userInfoDTO.getUserName());
        }

        // 密码长度限制
        if(userInfoDTO.getUserPassword().getBytes().length > AssetsEnum.MAX_PASSWORD_VARCHAR_LIMIT.value()){
            return new ResponseResult(2, "密码长度过长，字符大小需 <= 32Byte");
        }else if(userInfoDTO.getUserPassword().length() < AssetsEnum.MIN_PASSWORD_LENGTH_LIMIT.value()){
            return new ResponseResult(2, "密码长度需大于6位数");
        }else{
            entity.setUserPassword(userInfoDTO.getUserPassword());
        }

        // 用户权限
        if(userInfoDTO.getUserPopedom() == AssetsEnum.NORMAL_USER_MARK.value()){
            entity.setUserPopedom(userInfoDTO.getUserPopedom());
        }else if(userInfoDTO.getUserPopedom() == AssetsEnum.USER_LOCK_MARK.value()){
            entity.setUserPopedom(userInfoDTO.getUserPopedom());
        }else {
            entity.setUserPopedom(0);
        }

        // 联系方式字符大小限制
        if (userInfoDTO.getUserTelephone().getBytes().length > AssetsEnum.TELEPHONE_NUM_VARCHAR_LIMIT.value()){
            return new ResponseResult(2, "联系方式过长，字符大小需 <= 14Byte");
        }else {
            entity.setUserTelephone(userInfoDTO.getUserTelephone());
        }

        // 备注内容字符限制
        if(userInfoDTO.getUserRemark().getBytes().length > AssetsEnum.REMARK_VARCHAR_LIMIT.value()){
            return new ResponseResult(2, "备注内容过多，需 <= 120Byte");
        }else {
            entity.setUserRemark(userInfoDTO.getUserRemark());
        }

        return new ResponseResult(1,"成功",userInfoMapper.updateById(entity));
    }

    /**
     * 删除UserInfo
     * @param userId ID号
     * @return 返回当前当次删除状况(经ResponseResult包装类修饰)
     */
    @Override
    public ResponseResult deleteUserInfoById(int userId){
        int deleteNum = userInfoMapper.deleteById(userId);
        if (deleteNum == 0) {
            return new ResponseResult(0, "对象不存在");
        }
        return new ResponseResult(1, "成功");
    }
}
